#include<iostream>

struct TreeNode {
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
};

void Inorder(struct TreeNode* root, struct TreeNode*** ret, int* num)
{
    if (!root)
    {
        return;
    }
    else
    {
        *ret = (struct TreeNode**)realloc(*ret, sizeof(struct TreeNode*) * (++ * num));
        (*ret)[*num - 1] = root;
        Inorder(root->left, ret, num);
        Inorder(root->right, ret, num);
    }
}

void flatten(struct TreeNode* root) {
    int num = 0;
    struct TreeNode** ret = (struct TreeNode**)malloc(0);
    struct TreeNode* prev = NULL;
    Inorder(root, &ret, &num);
    for (int i = 0; i < num - 1; i++)
    {
        prev = ret[i];
        prev->left = NULL;
        prev->right = ret[i + 1];
    }
}